package com.sino.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sino.bean.Basicinfo;
import com.sino.bean.TblZhuanjiarukuxinxibiao;
import com.sino.bean.TblZhuanjiayuxiangmuguanxi;
import com.sino.dao.IExpertManageDAO;

public class ExpertManageDAO extends HibernateDaoSupport implements
		IExpertManageDAO {

	public List getExpertsReviewList(int firstRow, int maxRow, String hqlwhere) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getgetExpertsReviewCount(String hqlwhere) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List findExpertByName(String name) {
		String strSQL = "from TblZhuanjiarukuxinxibiao as tzjb where tzjb.FYhm=?";
		return this.getHibernateTemplate().find(strSQL, name);
	}

	// xyw查询项目根据专业码

	public List<Basicinfo> findSmByZhuanyema(String hql, String zhuanyema) {
		String strSQL = "from Basicinfo basic where basic.xk.id=?";
		if (null != hql) {
			strSQL = strSQL + " and " + hql;
		}
		return (List<Basicinfo>) this.getHibernateTemplate().find(strSQL,
				zhuanyema);
	}

	// xyw 查找所有
	public List<Basicinfo> findAllSms(String hql) {
		return (List<Basicinfo>) this.getHibernateTemplate().find(
				"from Basicinfo where " + hql);
	}

	// xyw 高级查找项目
	@SuppressWarnings("unchecked")
	public List<Basicinfo> findSms(String hqlWhere) {
		return (List<Basicinfo>) this.getSession().createQuery(hqlWhere).list();
	}

	@SuppressWarnings("unchecked")
	public List<TblZhuanjiarukuxinxibiao> findAllExperts() {
		// TODO Auto-generated method stub
		return (List<TblZhuanjiarukuxinxibiao>) this.getHibernateTemplate()
				.find("from TblZhuanjiarukuxinxibiao tzjb where tzjb.sysShenhezhuangtaima.bm is null");
	}

	// 高级查找 专家
	@SuppressWarnings("unchecked")
	public List<TblZhuanjiarukuxinxibiao> findExperts(String hqlWhere) {
		return (List<TblZhuanjiarukuxinxibiao>) this.getSession().createQuery(
				hqlWhere).list();
	}

	@SuppressWarnings("unchecked")
	public List<TblZhuanjiarukuxinxibiao> findThirdReviewExperts() {
		String hql="from TblZhuanjiarukuxinxibiao tzjb where tzjb.sysShenhezhuangtaima.bm='1'";
		hql=hql+"order by tzjb.FBh";
		return (List<TblZhuanjiarukuxinxibiao>)this.getHibernateTemplate().find(hql);
	}

	// 查询专家根据专业码
	public List<TblZhuanjiarukuxinxibiao> findExpertByZhuanyema(String zhuanyema) {
		String strSQL = "from TblZhuanjiarukuxinxibiao as tzjb where tzjb.sysZhuanyema.bm=?";
		return (List<TblZhuanjiarukuxinxibiao>) this.getHibernateTemplate()
				.find(strSQL, zhuanyema);
	}

	public List findExpertsAndBasicinfoByZlid(String id) {
		String strSQL = "from TblZhuanjiayuxiangmuguanxi as  tzjxm where tzjxm.basicinfo.zlid=?";
		return this.getHibernateTemplate().find(strSQL, id);
	}

	public void saveExperts(Object o) {
		this.getHibernateTemplate().saveOrUpdate(o);

	}

	public List findExpertsByZlid(String id) {
		String strSQL = "from TblZhuanjiayuxiangmuguanxi as tzjxm where  tzjxm.basicinfo.zlid=?";
		return this.getHibernateTemplate().find(strSQL, id);
	}

	public void saveExpertsAndBasicinfo(TblZhuanjiayuxiangmuguanxi tzjxm) {
		this.getHibernateTemplate().saveOrUpdate(tzjxm);

	}

	public TblZhuanjiarukuxinxibiao findExpertById(String id) {
		return (TblZhuanjiarukuxinxibiao) this.getHibernateTemplate().get(
				TblZhuanjiarukuxinxibiao.class, Integer.valueOf(id));

	}

	public List findExperts(final int firstRow, final int maxRow,
			String hqlwhere) {

		final String queryhql = hqlwhere;

		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryhql);
				query.setFirstResult(firstRow);
				query.setMaxResults(maxRow);
				List list = query.list();
				return list;
			}
		});
	}

	public int findExpertsCount(String hqlwhere) {

		return (Integer) this.getHibernateTemplate().find(hqlwhere).get(0);

	}

	public int findExpertReviewPageCount(String hqlwhere) {
		return (Integer) this.getHibernateTemplate().find(hqlwhere).get(0);

	}

	public List findExpertReviewPageList(final int firstRow, final int maxRow,
			String hqlwhere) {
		final String queryhql = hqlwhere;

		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryhql);
				query.setFirstResult(firstRow);
				query.setMaxResults(maxRow);
				List list = query.list();
				return list;
			}
		});

	}

	public void delExpertsAndBasicinfo(String zlid) {
		String strSQL1 = "from TblZhuanjiayuxiangmuguanxi as  tzjxm where tzjxm.basicinfo.zlid=?";
		String strSQL2 = "from TblZhuanjialinxuanjieguo as  tzjlx where tzjlx.basicinfo.zlid=?";
		List deabList = this.getHibernateTemplate().find(strSQL1, zlid);
		List deabList1 = this.getHibernateTemplate().find(strSQL2, zlid);
		this.getHibernateTemplate().deleteAll(deabList);
		this.getHibernateTemplate().deleteAll(deabList1);

	}

	public TblZhuanjiarukuxinxibiao findExpertsById(Integer zjid) {
		return (TblZhuanjiarukuxinxibiao) this.getHibernateTemplate().get(
				TblZhuanjiarukuxinxibiao.class, zjid);
	}
}
